package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐菜品关系控制层
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmeal_cache",key = "#setmealDto.getCategoryId() + '_' + #setmealDto.getStatus()")
    public R add(@RequestBody SetmealDto setmealDto) {

        return setmealService.addWithDishes(setmealDto);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R findPage(Integer page, Integer pageSize, String name) {

        return setmealService.findPage(page, pageSize, name);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmeal_cache", allEntries = true)  // 清楚setmeal_cache名称下，所有的缓存数据
    public R removeWithDishes(@RequestParam("ids") List<Long> ids) {

        return setmealService.deleteByids(ids);
    }


    /**
     * 查询回显
     *
     * @param setmealId
     * @return
     */
    @GetMapping("/{setmealId}")
    public R findById(@PathVariable("setmealId") Long setmealId) {

        return setmealService.findById(setmealId);
    }


    /**
     * 修改套餐
     *
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R modifySetmeal(@RequestBody SetmealDto setmealDto) {

        return setmealService.modifySetmeal(setmealDto);
    }


    /**
     * 批量修改状态
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R modifySetmealStatus(@PathVariable("status") Integer status, @RequestParam("ids") List<Long> ids) {

        return setmealService.modifySetmealStatus(status, ids);
    }

    /**
     * 根据分类ID和状态查询
     *
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmeal_cache", key = "#setmeal.getCategoryId()+'_' +#setmeal.getStatus()")
    public R list(Setmeal setmeal) {

        return setmealService.findAllSetmeal(setmeal);
    }


}